package com.zwy.leetcode;

import java.util.*;

public class Other {
    /**
     * 位1的个数
     * @param n
     * @return
     */
    public int hammingWeight(int n) {
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if (((n >>> i) & 1) == 1) {
                count++;
            }
        }
        return count;
    }

    /**
     * 汉明距离
     * @param x
     * @param y
     * @return
     */
    public int hammingDistance(int x, int y) {
        return 0;
    }

    /**
     * 缺失数字
     * @param nums
     * @return
     */
    public int missingNumber(int[] nums) {
        int length=nums.length;
        Set<Integer> set=new HashSet<Integer>();
        for (int i = 0; i < length; i++) {
            set.add(nums[i]);
        }
        for (int i = 0; i < length; i++) {
           if(!set.contains(i))return i;
        }

        return length;
    }



    /**
     * 有效的括号
     * @param s
     * @return
     */
    public boolean isValid(String s) {
        Stack<Character>stack=new Stack<Character>();
        char[] chars = s.toCharArray();
        stack.push(chars[0]);
        int i=1;
        while(i!=chars.length){
            char t=' ';
            if(!stack.isEmpty())t=stack.peek();
            if(fun(t,chars[i])){
                stack.pop();
            }
            else{
                stack.push(chars[i]);
            }
            i++;
        }
        return stack.isEmpty();
    }
    public boolean fun(char c1,char c2){
        if(c1=='('&&c2==')')return true;
//        if(c1==')'&&c2=='(')return true;
        if(c1=='{'&&c2=='}')return true;
//        if(c1=='}'&&c2=='{')return true;
        if(c1=='['&&c2==']')return true;
//        if(c1==']'&&c2=='[')return true;
        return false;
    }


    public List<List<Integer>> generate(int numRows) {
        return null;
    }



    public static void main(String[] args) {
        Other other=new Other();
        String s="(]";
        other.isValid(s);
    }

}
